$(document).ready(function() {
    dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: appBosch.crud.read,
                dataType: "json"
            },
            update: {
                url: appBosch.crud.update,
                dataType: "json"
            },
            parameterMap: function(options, operation) {
                var params = {};
                switch (operation)
                {
                    case 'read':
                        break;
                    case 'update':
                        params = {models: kendo.stringify(options.models)};
                        break;
                }
                return params;
            }
        },
        batch: true,
        schema: {
            model: {
                id: "id",
                fields: {
                    title: {
                        editable: true,
                        nullable: false,
                        validation: {
                            required: true
                        }
                    },
                    edition: {
                        editable: true,
                        nullable: false,
                        validation: {
                            required: true
                        }
                    },
                    emision_date: {
                        editable: false,
                        nullable: false,
                        type: "date",
                    },
                    revision_date: {
                        editable: true,
                        nullable: false,
                        type: "date",
                        validation: {
                            required: true
                        }
                    },
                    department: {
                        editable: false,
                        nullable: false,
                        validation: {
                            required: false
                        }
                    },
                    subtitle: {
                        editable: true,
                        nullable: false,
                        validation: {
                            required: true
                        }
                    },
                    author: {
                        editable: true,
                        nullable: false,
                        validation: {
                            required: true
                        }
                    },
                    author_department: {
                        editable: true,
                        nullable: true,
                        validation: {
                            required: true
                        }
                    },
                    author_number: {
                        editable: true,
                        nullable: false,
                        validation: {
                            required: true
                        }
                    },
                    author_extension: {
                        editable: true,
                        nullable: true
                    }
                }
            }
        }
    });

    $("#grid").kendoGrid({
        dataSource: dataSource,
        columns: [
            {
                field: "title",
                title: "Procedimiento",
                width: 200
            },
            {
                field: "subtitle",
                title: "Subtítulo",
                width: 150
            },
            {
                field: "edition",
                title: "Edición",
                width: 100
            },
            {
                field: "emision_date",
                title: "Emisión",
                width: 150,
                template: "#= kendo.toString(kendo.parseDate(emision_date), 'dd/MMM/yyyy')#"
            },
            {
                field: "revision_date",
                title: "Revisión",
                width: 150,
                template: "#= kendo.toString(kendo.parseDate(revision_date), 'dd/MMM/yyyy')#"
            },
            {
                field: "department",
                title: "Departamento",
                width: 150
            },
            {
                field: "author",
                title: "Autor",
                width: 150
            },
            {
                field: "author_department",
                title: "Departamento del autor",
                width: 150
            },
            {
                field: "author_number",
                title: "Número del Autor",
                width: 150
            },
            {
                field: "author_extension",
                title: "Extensión del Autor",
                width: 150
            },
            {
                command: [
                    {
                        name: "edit",
                        template: "<a title='Editar' class='k-button k-button-icontext k-grid-edit' href=''><i class='fa fa-pencil'></i></a>",
                        text: {
                            edit: "",
                            update: "",
                            cancel: ""
                        }
                    }
                ], title: "Editar", width: 80, locked: true
            }
        ],
        editable: {
            mode: "popup"
        }
    });
});
